package org.watchrecords.elements;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.watchrecords.WatchRecordsSession;
import org.watchrecords.constants.Role;
import org.watchrecords.dbo.User;
import org.watchrecords.pages.crime.EditIncidentPage;
import org.watchrecords.pages.crime.ViewIncidentsPage;
import org.watchrecords.pages.crime.ViewOffendersPage;
import org.watchrecords.pages.statistics.CellLogPage;
import org.watchrecords.pages.statistics.StatisticsPage;
import org.watchrecords.pages.user.UserListPage;

public class Sidebar extends Panel {
    private static class SidebarLink implements Serializable {
        private static final long serialVersionUID = 3783963979916919416L;
        public String title;
        public Class<? extends Page> page;
        
        public SidebarLink(String title, Class<? extends Page> page) {
            this.title = title;
            this.page = page;
        }
    }
    
    private static final List<SidebarLink> DEFAULT_LINKS = new ArrayList<SidebarLink>();
    private static final Map<Role, List<SidebarLink>> LINK_MAP = new HashMap<Role, List<SidebarLink>>();
    
    static {
        DEFAULT_LINKS.addAll(Arrays.asList(new SidebarLink[] {
                new SidebarLink("View incidents", ViewIncidentsPage.class),
                new SidebarLink("View offenders", ViewOffendersPage.class),
        }));
        
        LINK_MAP.put(Role.COURT, new ArrayList<SidebarLink>(DEFAULT_LINKS));
        LINK_MAP.get(Role.COURT).addAll(Arrays.asList(new SidebarLink[] {
                new SidebarLink("View cell log", CellLogPage.class),
                new SidebarLink("Statistics", StatisticsPage.class),
                new SidebarLink("User list", UserListPage.class),
        }));

        LINK_MAP.put(Role.WATCHMAN, new ArrayList<SidebarLink>(Arrays.asList(new SidebarLink[] {
                new SidebarLink("Create incident", EditIncidentPage.class),
        })));
        LINK_MAP.get(Role.WATCHMAN).addAll(LINK_MAP.get(Role.COURT));
        
        LINK_MAP.put(Role.ADMINISTRATOR, new ArrayList<SidebarLink>(LINK_MAP.get(Role.WATCHMAN)));
    }
    
    /**
     * 
     */
    private static final long serialVersionUID = 735660615268961615L;

    public Sidebar(String id) {
        super(id);
        
        WatchRecordsSession session = (WatchRecordsSession) Session.get();
        User user = session.getUser();
        List<SidebarLink> linkList = (user == null) ? DEFAULT_LINKS : LINK_MAP.get(user.getRole());
        
        add(new ListView<SidebarLink>("linkList", linkList) {
            private static final long serialVersionUID = 5335420117139972760L;

            @Override
            protected void populateItem(ListItem<SidebarLink> item) {
                item.add(new PageLink<Object>("link", item.getModelObject().page).add(new Label("linkText", item.getModelObject().title)));
            }
        });
    }

}
